var Server = {
    /* Callback function to be set by client */
    videoDataReceivedCallback : null,
	videoDataFailedCallback : null,
    videoTimeoutId : null,
	
    XHRVideoObj : null,
    xmlUrl : "http://192.168.1.88/text.xml",
    tag: "Server.js"
}

Server.init = function(){
    var success = true;

	if(!this.xmlUrl) {
		success = false;
	}else{
		
		if (this.XHRVideoObj){
			this.XHRVideoObj.destroy();  // Save memory
			this.XHRVideoObj = null;
		}
		
	}
    return success;
}

Server.callInProgress = function(xhr) {
    //alert("callInProgress, state: " + xhr.readyState);
    switch ( xhr.readyState ) {
        case 1:
        case 2:
        case 3:
        return true;
        break;

        // Case 4 and 0
        default:
        return false;
        break;
    }
}

Server.timer = function(xhr) {
    Server.timeoutId = setTimeout(
        function() {
            if ( Server.callInProgress( xhr ) ) {
                xhr.abort();
				alert("xhr aborted!");
            } else {
                alert("xhr not aborted!");
            }
        },
        10000 // 10 seconds
    );
}

Server.fetchVideoList = function(){
	//alert("Fetch video list");

    if (this.XHRVideoObj == null){
		//alert("XHRVideoObj is null - creating a new one");
        this.XHRVideoObj = new XMLHttpRequest();
    }
	
    this.XHRVideoObj = new XMLHttpRequest(); 
    
	if (this.XHRVideoObj){
		//alert("XHRVideoObj exists");
		
        this.XHRVideoObj.onreadystatechange = function(){
			//alert("Ready state change");
		
                if (Server.XHRVideoObj.readyState == 4){ // if request was ended
					clearTimeout(Server.videoTimeoutId);
					Server.videoTimeoutId = null;
                    Server.createVideoList();
					//alert("Is ready indeed!");
                }
				
            }
        Server.videoTimeoutId = setTimeout(
            function() {
                alert("videoTimeout");
                if ( Server.callInProgress(Server.XHRVideoObj) ) {
                    Server.abortVideoList();
                    alert("XHRVideoObj aborted!");
                    if (Server.videoDataFailedCallback){
                        Server.videoDataFailedCallback();  
                    }
                } 
            },
            10000 // 10 seconds
        );
            
        this.XHRVideoObj.open("GET", this.xmlUrl, true);
        this.XHRVideoObj.send(null);
	}else{
		alert("Failed to create XHR");
	}
}

Server.abortVideoList = function(){
	if(Server.XHRVideoObj){
		alert("abortVideoList");
		Server.XHRVideoObj.abort();
		Server.XHRVideoObj.onreadystatechange = null;
		clearTimeout(Server.videoTimeoutId);
		Server.videoTimeoutId = null;
	}
}

Server.createVideoList = function(){
    if (this.XHRVideoObj.status != 200){
		alert("Http status: " + this.XHRVideoObj.status);
        if (this.videoDataFailedCallback){
            this.videoDataFailedCallback();  
        }
		
    }else{
        var xmlElement = this.XHRVideoObj.responseXML.documentElement;
        
        if (!xmlElement){
            alert("Failed to get valid XML");
        }else{
		
            // Get all "item" elements
            var items = xmlElement.getElementsByTagName("item");
            
            for (var index = 0; index < items.length; index++){
			
				Looper.parseItem(items[index]);
			
            }
        
            if (this.videoDataReceivedCallback){
                this.videoDataReceivedCallback();    /* Notify all data is received and stored */
            }
        }
		
    }
}
